using System;
using System.Collections.Generic;
using System.Data;

namespace Glpt.Data
{
    public class ColumnMap
    {
        /// <summary>
        /// 映射方式 0按名称 1按顺序 2按大写名称 3按小写名称
        /// </summary>
        public string MAP_MODE = "0";
        /// <summary>
        /// 配对方式 0自动配对 1指定配对
        /// </summary>
        public string PAIR_MODE = "0";
        /// <summary>
        /// 字段列表
        /// </summary>
        public Dictionary<string, string> COLUMN_LIST = null;
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public ColumnMap()
        {
            COLUMN_LIST = new Dictionary<string, string>();
        }
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="param_value">参数</param>
        public ColumnMap(string param_value)
        {
            COLUMN_LIST = new Dictionary<string, string>();
            string[] vals = param_value.Split(new char[] { ',' }, StringSplitOptions.None);
            if (vals.Length > 0) MAP_MODE = vals[0];
            if (vals.Length > 1) PAIR_MODE = vals[1];
            for (int i = 2; i < vals.Length; i++)
            {
                if (vals[i].Length == 0) continue;
                int idx = vals[i].IndexOf(":");
                string key = vals[i].ToUpper();
                string value = "";
                if (idx > 0)
                {
                    key = vals[i].Substring(0, idx).ToUpper();
                    value = vals[i].Substring(idx + 1);
                }
                if (!COLUMN_LIST.ContainsKey(key))
                    COLUMN_LIST.Add(key, value);
            }
        }

        /// <summary>
        /// 获取目标字段名
        /// </summary>
        /// <param name="column_id">源字段序号</param>
        /// <param name="column_name">源字段名</param>
        /// <returns>目标字段名</returns>
        public string GetDestinationColumnName(int column_id, string column_name)
        {
            if (MAP_MODE.Equals("1"))
            {
                int destId = GetDestinationColumnId(column_id);
                if (destId >= 0)
                {
                    if (column_name.Length == 0) column_name = "COL" + (column_id + 1).ToString();
                }
                else
                    column_name = "";
            }
            else
            {
                column_name = column_name.Length > 0 ? GetDestinationColumn(column_name) : "COL" + (column_id + 1).ToString();
            }
            return column_name;
        }
        /// <summary>
        /// 获取目标字段名
        /// </summary>
        /// <param name="column_name">源字段名</param>
        /// <returns>目标字段名</returns>
        public string GetDestinationColumn(string column_name)
        {
            string destinationColumn = "";
            string column_name_up = column_name.ToUpper();
            if (PAIR_MODE.Equals("1"))//指定配对
            {
                if (COLUMN_LIST.ContainsKey(column_name_up))
                {
                    destinationColumn = COLUMN_LIST[column_name_up];
                }
            }
            else//自动配对
            {
                int idx = column_name.LastIndexOf(".");
                if (idx >= 0 && COLUMN_LIST.ContainsKey("."))
                {
                    column_name = column_name.Substring(idx + 1);
                    column_name_up = column_name.ToUpper();
                }
                destinationColumn = column_name;//1
                if (COLUMN_LIST.ContainsKey(column_name_up))
                    destinationColumn = COLUMN_LIST[column_name_up];
                else if (MAP_MODE.Equals("2"))
                    destinationColumn = column_name_up;
                else if (MAP_MODE.Equals("3"))
                    destinationColumn = column_name.ToLower();
            }
            return destinationColumn;
        }
        /// <summary>
        /// 获取目标字段序号
        /// </summary>
        /// <param name="column_id">源字段序号</param>
        /// <returns>目标字段序号</returns>
        public int GetDestinationColumnId(int column_id)
        {
            int destinationColumnId = -1;
            if (PAIR_MODE.Equals("1"))//指定配对
            {
                if (COLUMN_LIST.ContainsKey(column_id.ToString()))
                {
                    destinationColumnId = int.Parse(COLUMN_LIST[column_id.ToString()]);
                }
            }
            else//自动配对
            {
                destinationColumnId = column_id;
                if (COLUMN_LIST.ContainsKey("*"))
                    destinationColumnId += int.Parse(COLUMN_LIST["*"]);
                if (COLUMN_LIST.ContainsKey(column_id.ToString()))
                    destinationColumnId = int.Parse(COLUMN_LIST[column_id.ToString()]);
            }
            return destinationColumnId;
        }
        /// <summary>
        /// 获取字段映射字符串
        /// </summary>
        /// <param name="reader">数据流</param>
        /// <returns>映射字符串</returns>
        public string GetMapMode(IDataReader reader)
        {
            return GetMapMode(reader, null);
        }
        /// <summary>
        /// 获取字段映射字符串
        /// </summary>
        /// <param name="table">数据表</param>
        /// <returns>映射字符串</returns>
        public string GetMapMode(DataTable table)
        {
            return GetMapMode(null, table);
        }

        private string GetMapMode(IDataReader reader, DataTable table)
        {
            string map_params = MAP_MODE;
            int FieldCount = (reader != null ? reader.FieldCount : table.Columns.Count);
            for (int i = 0; i < FieldCount; i++)
            {
                if (MAP_MODE.Equals("1"))
                {
                    int destId = GetDestinationColumnId(i);
                    if (destId >= 0)
                        map_params += "," + i.ToString() + ":" + destId.ToString();
                }
                else
                {
                    string column_name = (reader != null ? reader.GetName(i) : table.Columns[i].ColumnName);
                    string destinationColumn = GetDestinationColumn(column_name);
                    if (destinationColumn.Length > 0)
                        map_params += "," + column_name + ":" + destinationColumn;
                }
            }
            return map_params;
        }
    }
}
